package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.entity.Salary;
import com.qfedu.hotelssm.service.intf.SalaryService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/salary/")
public class SalaryController {
    @Autowired
    private SalaryService service;

    @PostMapping("save.do")
    public R save(Salary salary){
        return service.save(salary);
    }

    @PostMapping("delids.do")
    public R deleteByIds(int[] ids){
        return service.deleteByIds(ids);
    }

    @GetMapping("delete.do")
    public R delete(int id){
        return service.delete(id);
    }

    @PostMapping("update.do")
    public R update(Salary salary){
        return service.update(salary);
    }

    @GetMapping("page.do")
    public PageBean selectPage(String name, int page, int limit){
        return service.selectPage(name, page, limit);
    }
    //查询全部 优化自动选值
    @GetMapping("all.do")
    public R all(){
        return service.all();
    }
    //导出
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        List<Salary> list = service.download();
        response.setHeader("content-disposition", "attachment;filename=salary"+System.currentTimeMillis()+".xlsx");
        EasyExcel.write(response.getOutputStream(),Salary.class).sheet("工资表").doWrite(list);
    }
    //导入
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }
    //查询员工ID 名称 部门
    @PostMapping("dto.do")
    public R allDto(){
        return service.allDto();
    }
    //部门分组 查询总工资
    @GetMapping("sum.do")
    public R sumSalary(){
        return service.sumSalary();
    }
    //显示各部门内男女工资
    @GetMapping("sex.do")
    public R sumSexSalary(){
        return service.sumSexSalary();
    }
}
